package com.bmmtwabp.employeedashbord.controller;

import com.bmmtwabp.employeedashbord.domain.Heat;
import com.bmmtwabp.employeedashbord.service.HeatService;
import com.bmmtwabp.employeedashbord.util.*;
import com.bmmtwabp.employeedashbord.vo.*;
import com.github.pagehelper.*;
import io.swagger.annotations.*;
import java.util.*;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: -Controller
 * @Param:
 * @return:
 * @Author: fenghouzhi
 * @Date: 2018/8/27
 */
@Slf4j
@Controller
@Api(tags = "热饭接口")
public class HeatController extends BaseController {

  @Resource
  private HeatService heatService;

  /**
   * 进入热饭页面
   */
  @ApiOperation(value = "后台-进入热饭列表页面")
  @GetMapping("/heat")
  String toHeatingHtml() {
    log.info("进入热饭页面!");
    return "heating";
  }

  /**
   * 获取热饭列表接口
   */
  @ApiOperation(value = "后台-热饭列表接口")
  @GetMapping("/heats")
  @ResponseBody
  Map<String, Object> getHeatVoList(QueryRequest request, QueryHeatVo queryHeatVo) {
    PageHelper.startPage(request.getPageNum(), request.getPageSize());
    List<HeatVo> heatVoList = heatService.getHeatVoList(queryHeatVo);
    PageInfo<HeatVo> pageInfo = new PageInfo<>(heatVoList);
    log.info("response info: ---  " + pageInfo);
    return getDataTable(pageInfo);
  }

  /**
   * 获取正在热饭的用户昵称
   */
  @ApiOperation(value = "后台-获取正在热饭的用户昵称")
  @GetMapping("/heat/heating/{zoneName}")
  @ResponseBody
  ResponseVo getHeatingNickName(@PathVariable String zoneName) {
    if (zoneName == null || "".equals(zoneName)) {
      return ResponseVo.warn("传参为null！");
    }

    String heatingNickName = heatService.getHeatingNickName(zoneName);
    if (heatingNickName != null && !"".equals(heatingNickName)) {
      return ResponseVo.ok("获取成功!", heatingNickName);
    } else {
      return ResponseVo.warn("当前没有人热饭");
    }

  }

  /**
   * 后台结束热饭
   */
  @ApiOperation(value = "后台-结束热饭")
  @PostMapping("/heat/over")
  @ResponseBody
  ResponseVo overHeatByHeadId(Long heatId) {
    if (heatId == null || "".equals(heatId)) {
      return ResponseVo.warn("传参为null！");
    }

    Integer result = heatService.overHeatByHeatId(heatId);
    if (result == 1) {
      return ResponseVo.ok("结束成功", null);
    } else {
      return ResponseVo.error("结束失败");
    }

  }

  /**
   * 获取等待热饭人数
   */
  @ApiOperation(value = "后台-获取等待热饭人数")
  @GetMapping("/heat/wait/{zoneName}")
  @ResponseBody
  ResponseVo getWaitHeatSum(@PathVariable String zoneName) {

    if (zoneName == null || "".equals(zoneName)) {
      return ResponseVo.warn("传参为null！");
    }

    Integer waitHeatSum = heatService.getWaitHeatSum(zoneName);
    return ResponseVo.ok("获取成功!", waitHeatSum);
  }

  /**
   * 获取历史数据-总热饭人数
   */
  @ApiOperation(value = "后台-获取历史数据-总热饭人数")
  @GetMapping("/heat/history/{zoneName}")
  @ResponseBody
  ResponseVo getHistoryHeatSum(@PathVariable String zoneName) {

    if (zoneName == null || "".equals(zoneName)) {
      return ResponseVo.warn("传参为null！");
    }

    Integer heatSum = heatService.getHistoryHeatSum(zoneName);
    return ResponseVo.ok("获取成功!", heatSum);
  }

  /**
   * 小程序-获取结束热饭列表
   */
  @ApiOperation(value = "小程序-热饭列表")
  @PostMapping("/app/heats")
  @ResponseBody
  ResponseVo getHeatListForApp(@RequestBody AppQueryHeatListVo appQueryHeatListVo) {
    String openId = appQueryHeatListVo.getOpenId();
    String zoneName = appQueryHeatListVo.getZoneName();
    log.info("openId:   " + openId + "  zone:   " + zoneName);

    if (openId == null || "".equals(openId) || zoneName == null || "".equals(zoneName)) {
      return ResponseVo.error("查询参数未传入!!!");
    }

    heatService.findTimeOutMethod(zoneName);
    String heatStatus = heatService.getUserHeatStatusByZoomName(openId, zoneName);
    String heatWaitOpenId;
    if (heatService.getReadyByOpenId(zoneName) != null) {
      heatWaitOpenId = heatService.getReadyByOpenId(zoneName).getOpenId();
    } else {
      heatWaitOpenId = "";
    }
    List<HeatVo> heatVoList = heatService.getHeatListForApp(zoneName);

    Map<String, Object> resultMap = new HashMap<>(3);
    if (heatVoList != null && heatVoList.size() > 0) {
      resultMap.put("heatStatus", heatStatus);
      resultMap.put("heatReadyOpenId", heatWaitOpenId);
      resultMap.put("heatVoList", heatVoList);
      return ResponseVo.ok("获取成功", resultMap);
    } else {
      return ResponseVo.ok("列表为空", null);
    }
  }

  /**
   * 小程序-进入热饭等待队列
   */
  @ApiOperation(value = "进入热饭等待队列")
  @PostMapping("/app/heat")
  @ResponseBody
  ResponseVo insertHeatInfo(@RequestBody Heat heat) {
    log.info("新增热饭 info：  " + heat);

    if (heat.getOpenId() == null || "".equals(heat.getOpenId())) {
      return ResponseVo.warn("openId为空！！");
    }

    Integer result = heatService.insertHeat(heat);
    log.info("info: " + result);
    Map resultMap = new HashMap(2);
    if (result == 1) {
      resultMap.put("heatStatus", result);
      return ResponseVo.ok("可以准备热饭了！！", resultMap);
    } else if (result == 0) {
      resultMap.put("heatStatus", 0);
      return ResponseVo.ok("排队成功！", resultMap);
    } else {
      return ResponseVo.error("排队异常！");
    }

  }

  /**
   * 小程序-开始热饭
   */
  @ApiOperation(value = "开始热饭")
  @PostMapping("/app/heat/start")
  @ResponseBody
  ResponseVo startHeat(@RequestBody HeatOperationVo heatOperationVo) {
    String openId = heatOperationVo.getOpenId();
    String zoneName = heatOperationVo.getZoneName();
    log.info("startHeat openid info:    " + openId + "  startHeat zoneName info:  " + zoneName);

    if (openId == null || "".equals(openId)) {
      return ResponseVo.error("openid未传入!!!");
    }

    Integer result = heatService.startHeat(openId, zoneName);
    if (result == 1) {
      Map<String, Integer> resultMap = new HashMap<>(2);
      resultMap.put("heatStatus", 2);
      return ResponseVo.ok("您可以热饭了！", resultMap);
    } else {
      return ResponseVo.error("您热饭出现点儿问题哦~~");
    }
  }

  /**
   * 小程序-结束热饭
   */
  @ApiOperation(value = "结束热饭")
  @PostMapping("/app/heat/over")
  @ResponseBody
  ResponseVo overHeat(@RequestBody HeatOperationVo heatOperationVo) {
    String openId = heatOperationVo.getOpenId();
    String zoneName = heatOperationVo.getZoneName();
    log.info("overHeat openid info:    " + openId + "  overHeat zoneName info:    " + zoneName);

    if (openId == null || "".equals(openId)) {
      return ResponseVo.error("openid未传入!!!");
    }

    Integer result = heatService.overHeat(openId, zoneName);
    if (result == 1) {
      Map<String, Integer> resultMap = new HashMap<>(1);
      resultMap.put("heatStatus", 3);
      return ResponseVo.ok("您的饭好了！用餐愉快!!", resultMap);
    } else {
      return ResponseVo.error("您未正常结束热饭~~");
    }

  }

  /**
   * 小程序-取消热饭
   */
  @ApiOperation(value = "取消热饭")
  @DeleteMapping("/app/heat/cancel")
  @ResponseBody
  ResponseVo cancelHeat(@RequestBody HeatOperationVo heatOperationVo) {
    String openId = heatOperationVo.getOpenId();
    String zoneName = heatOperationVo.getZoneName();
    log.info("canelHeat openid info:    " + openId + "  overHeat zoneName info:    " + zoneName);

    if (openId == null || "".equals(openId)) {
      return ResponseVo.error("openid未传入!!!");
    }

    Integer result = heatService.cancelHeat(openId, zoneName);
    if (result == 1) {
      Map<String, Object> resultMap = new HashMap<>(1);
      resultMap.put("heatStatus", "");
      return ResponseVo.ok("您成功取消热饭", resultMap);
    } else {
      return ResponseVo.error("取消热饭异常~~");
    }

  }

}
